package com.jbm.jbmsupplier.activity.address.presenter;

import android.content.Context;
import android.view.View;

import com.jbm.enterapplication.InterfaceMa;
import com.jbm.jbmsupplier.activity.address.MyAddRessRepair;
import com.jbm.jbmsupplier.activity.address.mode.MyAddressMode;
import com.jbm.jbmsupplier.activity.address.view.MyAddressViewInterface;
import com.jbm.jbmsupplier.adapter.MyRecy_photoAdapter;
import com.jbm.jbmsupplier.view.DialoagButtonLoading;
import com.jbm.jbmsupplier.view.DoubleButtonDialog;
import com.jbm.onehome.iface.Media;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by 家帮忙 on 2017/9/28.
 */

public class MyAddressReClass implements  MyaddressInterface {
    private Context context;
    private MyAddressViewInterface myAddressViewInterface;
    private MyAddressMode myAddressMode;
    private DialoagButtonLoading dialoagButtonLoading;
    MyRecy_photoAdapter myRecy_photoAdapter;
    ArrayList<HashMap<String, String>> listrecyss;
int tag;
    private String orderid;



    public MyAddressReClass(Context context, MyAddressViewInterface myAddressViewInterface) {
        this.context = context;
        this.myAddressViewInterface = myAddressViewInterface;
        myAddressMode=new MyAddressMode();
        dialoagButtonLoading=new DialoagButtonLoading(context);
    }

    /**
     * 提交订单
     * 图像可以为空
     */
    @Override
    public void SubmitMassage() {
        dialoagButtonLoading.Loadingshow("提交中请等待",true);
        myAddressMode.DisposeOrder(myAddressViewInterface.getPhoto(), myAddressViewInterface.getAddress(), myAddressViewInterface.getUser(), myAddressViewInterface.getDesc(), myAddressViewInterface.getMaintain(), new InterfaceMa<String>() {
            @Override
            public void setError(String err, boolean b) {
                dialoagButtonLoading.disims();
                dialoagButtonLoading.hint(err);
            }

            @Override
            public void onSuccess(String s, boolean b) {
                orderid=s;
                 tag=0;
                if(listrecyss!=null){
                if (listrecyss.size()>0){
                for (int i = 0; i <listrecyss.size() ; i++) {
                if (listrecyss.get(i).get("audio").equals("pic")){
                    Media   media=  new Media();
                    media.mtype = "pic";
                    media.filepath = listrecyss.get(i).get("path");
                    media.filename = listrecyss.get(i).get("name");
                    SubmitPic( media);
                }else  if (listrecyss.get(i).get("audio").equals("audio")){
                    Media   media=  new Media();
                    media.mtype = "audio";
                    media.filepath = listrecyss.get(i).get("path");
                    media.filename = listrecyss.get(i).get("name");
                    SubmitPic( media);
//                    SubitAudio();
                }else if (listrecyss.get(i).get("audio").equals("video")){
                    Media    media=  new Media();
                    media.mtype = "video";
                    media.filepath = listrecyss.get(i).get("path");
                    media.filename = listrecyss.get(i).get("name");
                    SubmitPic( media);
//                    SubmitVideo();
                }
                  }
                    }else {
                    dialoagButtonLoading.disims();
                }
                      }else{
                    dialoagButtonLoading.disims();
                    ((MyAddRessRepair) context).finish();
                }

            }
        });
    }

    /**
     * 提交图片文件
     * @param media
     */
    @Override
    public void SubmitPic(Media media) {
        if (media!=null){
        myAddressMode.DisposePic(orderid, media, new InterfaceMa<String>() {
            @Override
            public void onSuccess(String o, boolean b) {
                tag++;
                if (tag == listrecyss.size()) {
                    dialoagButtonLoading.disims();
                    ((MyAddRessRepair) context).finish();
                }


            }

            @Override
            public void setError(String err, boolean b) {
                dialoagButtonLoading.disims();
                dialoagButtonLoading.hint(err);
            }
        });}
    }

    @Override
    public void SubmitVideo() {
        dialoagButtonLoading.Loadingshow("提交中请等待",true);
//        myAddressMode.Disposevedio(orderid, media, new InterfaceMa<String>() {
//            @Override
//            public void onSuccess(String o, boolean b) {
//                dialoagButtonLoading.disims();
//            }
//
//            @Override
//            public void setError(String err, boolean b) {
//                dialoagButtonLoading.disims();
//                dialoagButtonLoading.hint(err);
//            }
//        });
    }

    @Override
    public void SubitAudio() {
//        myAddressMode.Disposeaudio(orderid, media, new InterfaceMa<String>() {
//            @Override
//            public void onSuccess(String o, boolean b) {
//                dialoagButtonLoading.disims();
//            }
//
//            @Override
//            public void setError(String err, boolean b) {
//                dialoagButtonLoading.disims();
//                dialoagButtonLoading.hint(err);
//            }
//        });
    }

    /**
     * 获取文件的路径
     *设置数据
     * @param listrecys
     */

    @Override
    public void setUpdate(ArrayList<HashMap<String, String>> listrecys) {
        if (listrecys.size()>0){
            myAddressViewInterface.getrecycler().setVisibility(View.VISIBLE);
            if (listrecys.size()>4){
                hint("不支持再多任务");
            }else {
                if (myRecy_photoAdapter == null) {
                    myRecy_photoAdapter = new MyRecy_photoAdapter(context, listrecys,new OnclicAdapter(){
                        @Override
                        public void getDaTaList(ArrayList<HashMap<String, String>> listrecys) {
                            if (listrecys.size()==0){
                                myAddressViewInterface.getrecycler().setVisibility(View.GONE);
                            }else {
                            listrecyss=listrecys;
                        }}
                    });
                    myAddressViewInterface.getrecycler().setAdapter(myRecy_photoAdapter);
                } else {
                    myRecy_photoAdapter.setNotifi(listrecys);
                }


            }
        }


    }

    /**
     * 设置数据接口回掉
     */
    public interface OnclicAdapter{
    void getDaTaList(ArrayList<HashMap<String, String>> listrecys);


}

    //一些提示的时候调用
    public void hint(String name) {
        final DoubleButtonDialog dialog = new DoubleButtonDialog(context, name);
        dialog.getokbtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setisVisibility();

    }







}
